package org.ukpass.local;

import java.util.List;

import org.dom4j.Element;
import org.ukpass.esp_nisk.R;
import org.ukpass.util.DirectoryParse;
import org.ukpass.util.NiskConstant;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LocalSelectAdapter extends BaseAdapter {
	private Context context;
	private List<Element> items;
	private LayoutInflater inflater;

	public LocalSelectAdapter(Context context, List<Element> items) {
		inflater = LayoutInflater.from(context);
		this.context = context;
		this.items = items;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int id) {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = new ViewHolder();
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.local_select_item, null);
			holder.fileIcon = (ImageView) convertView
					.findViewById(R.id.file_icon);
			holder.fileName = (TextView) convertView
					.findViewById(R.id.file_name_text);
			holder.fileAttr = (TextView) convertView
					.findViewById(R.id.file_attr_text);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		String type = items.get(position).attributeValue("type");
		String fileTime = items.get(position).attributeValue("time");
		String fileName = items.get(position).attributeValue("labels");
		
		
		if(type.equals("1"))
		{
			holder.fileIcon.setImageResource(R.drawable.list_icon_my_received_folder);
			holder.fileAttr.setText(fileTime);
		}else if(type.equals("2"))
		{
			try {
				String fileType = fileName.substring(
						fileName.lastIndexOf(".") + 1, fileName.length());
				Log.i("-----------", fileType);
				if (fileType.equals("zip"))
					holder.fileIcon.setImageResource(R.drawable.zip);
				else if (fileType.equals("txt") || fileType.equals("log")
						|| fileType.equals("xml") || fileType.equals("conf")) {
					holder.fileIcon.setImageResource(R.drawable.txt);
				} else if (fileType.equals("exe")) {

				} else if (fileType.equals("doc")|| fileType.equals("docx")) {
					holder.fileIcon.setImageResource(R.drawable.doc);
				}else if(fileType.equals("mp3") || fileType.equals("ape"))
				{
					holder.fileIcon.setImageResource(R.drawable.mp3);
				}else if(fileType.equals("mp4")||fileType.equals("rmvb")||fileType.equals("rmv")||fileType.equals("avi")||fileType.equals("3gp"))
				{
					holder.fileIcon.setImageResource(R.drawable.avi);
				}
				else if(fileType.equals("png")||fileType.equals("jpeg")||fileType.equals("icon")){
					holder.fileIcon.setImageResource(R.drawable.png);
				}else if(fileType.equals("apk"))
				{
					holder.fileIcon.setImageResource(R.drawable.apk);
				}
				else {
					holder.fileIcon.setImageResource(R.drawable.unknow);
				}
			} catch (StringIndexOutOfBoundsException e) {
				/**
				 * @Description uknow file type
				 */
			}
			holder.fileAttr.setText(fileTime);
		}else if(type.equals("3"))
		{
			holder.fileIcon.setImageResource(R.drawable.back);
			holder.fileAttr.setText(DirectoryParse.getXMLAbsolutePath(NiskConstant.selectNiskElement));
		}
		
		holder.fileName.setText(fileName);
		
		

		return convertView;
	}

	private class ViewHolder {
		ImageView fileIcon;
		TextView fileName;
		TextView fileAttr;
	}

}
